package com.crm.system.service.impl.internalTrainingManager;

import cn.hutool.core.util.ObjectUtil;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.internalTrainingManager.NxContact;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.internalTrainingManager.NxContactQO;
import com.crm.service.internalTrainingManager.NxContactDbService;
import com.crm.system.service.internalTrainingManager.NxContactService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 内训联系人
 * @Author renshaorong
 * @Date 2021/9/14
 */
@Service
public class NxContactServiceImpl implements NxContactService {

    @Autowired
    NxContactDbService nxContactDbService;


    @Override
    public DhPageInfo<NxContact> selectContactVOListByCustomerId(Integer pageIndex, Integer pageSize, NxContactQO nxContactQO) {

        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        List<NxContact> nxContacts = nxContactDbService.selectByCustomerId(nxContactQO);
        PageInfo<NxContact> pageInfo = new PageInfo<NxContact>(nxContacts);
        return new DhPageInfo<NxContact>(pageInfo, nxContacts);
    }

    @Override
    public boolean insertNxContact(NxContact nxContact) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        nxContact.setContactId(CommonUtils.getGUID());
        nxContact.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        nxContact.setCreatePeopleId(userAccount.getAccountId());
        nxContact.setCreateTm(new Date());
        return nxContactDbService.insertNxContact(nxContact);
    }

    @Override
    public List<NxContact> getNxContactByOwner(NxContactQO nxContactQO) {
        return nxContactDbService.selectByCustomerId(nxContactQO);
    }

    @Override
    public String selectNxContactByContactInformation(String contactInformation,String customerId){
        if (contactInformation.startsWith("0")) {
            contactInformation = contactInformation.substring(1);
        }
        NxContact nxContact = nxContactDbService.selectNxContactByContactInformation(contactInformation,customerId);
        if(ObjectUtil.isNotNull(nxContact)){
            return nxContact.getContactName();
        }
        return null;
    }
}